﻿using Microsoft.EntityFrameworkCore;
using Rj.PersonnelVerification.Server.DataBaseContext;
using Rj.PersonnelVerification.Server.DataBaseContext.Entities;
using Rj.PersonnelVerification.Shared.Models.Request;
using System.IO;
using System.Linq;

namespace Rj.PersonnelVerification.Server.Repositories.Impl
{
    public class VerificationRepository :  IVerificationRepository
    {
        private HisDbContext hisDbContext;
        public VerificationRepository(HisDbContext hisDbContext)
        {
            this.hisDbContext = hisDbContext;
        }

        public async Task<IEnumerable<IoTbWitnessVerification>> QueryByExample(QueryNoUploadRequest request)
        {
            return await hisDbContext.IoTbWitnessVerifications.Join(hisDbContext.IoTbInPatients, verif => verif.RegNo, pati => pati.RegNo, (verif, pati) => new { verif, pati }).Where(s => (s.pati.HospNo == request.HospNo || string.IsNullOrWhiteSpace(request.HospNo))
                    && (s.pati.OutTime > request.BeginTime || !request.BeginTime.HasValue)
                    && (s.pati.OutTime < request.EndTime || !request.EndTime.HasValue) && s.pati.InsuranceCode == 111&& s.verif.ResJson.Contains("success\":true")).Select(s => s.verif).ToListAsync();
        }
    }
}
